import { Notification,Loading } from 'element-ui';
import Cookies from 'js-cookie';
import Vue from 'vue';
export default function ({ $axios, redirect }) {

  //$axios.defaults.withCredentials = true;
  // $axios.defaults.baseURL = 'http://124.70.76.191:9030'
  // $axios.defaults.baseURL = 'http://124.70.76.191:9001'
   // $axios.defaults.baseURL = 'http://192.168.0.115:9001'
   $axios.defaults.baseURL = 'https://hw2.cswzsoft.com:9001'
   //独立版部署需要本地配置文件配置后端接口的时候需要放开此注释
   // $axios.defaults.baseURL = Vue.prototype.$base_host

  $axios.onRequest(config => {
    //独立版部署需要本地配置文件配置后端接口的时候需要放开此注释
    // $axios.defaults.baseURL = Vue.prototype.$base_host
    // config.baseURL = Vue.prototype.$base_host
    // console.log(config,77)
    // var url = config.url;
    // if(url.indexOf("?") != -1 ){
    //   url +=  "&token="+Cookies.get("token");
    // }
    // else{
    //   url +=  "?token="+Cookies.get("token");
    // }
    // config.url = url;
    // const qs = require('qs');
    // config.data = qs.stringify(config.data);
    // if(config.method === 'post') {

    // }
    if(Cookies.get("token")){
      config.headers.common['token']=Cookies.get("token");
    }
    //config.headers['Content-Type']='application/json'
  })
  $axios.onResponse(response => {
    // 每次手动关闭所有弹框
  	if(response.data.code!="0" && response.data.msg){
        Notification.closeAll()
        Notification.error({
          title:"提示",
          duration: 1700,
          message:response.data.msg
        });
      if(response.data.code=="20002"){
        Cookies.remove('token');
        Cookies.remove('userData');
        Cookies.remove('mobile');
        Cookies.remove('nickName');
        Cookies.remove('photo');
        Cookies.set('login','no',{ expires: 1 });
        if(Cookies.get('sysType') == 'exam'){
          redirect("/login_exam");
        }else{
          redirect("/login");
        }

      }
      // else if(response.data.code=="20002"){

      //   redirect("/login");
      // }

      return Promise.reject(response);
    }
  })

  $axios.onError(error => {
    // const code = parseInt(error.response && error.response.status)
    // if (code === 400) {
    //   redirect('/400')
    // }
  })
}
